﻿namespace LeRenard.ScriptWars.Model.GridSystem
{
    /// <summary>
    /// Represents a grid cell.
    /// </summary>    
    public class GridCell<TValue> : IGridCell<TValue>
        where TValue : class
    {
        /// <summary>
        /// Event fired when the value has been changed.
        /// </summary>
        public event GridCellValueChangedEventHandler<TValue> OnValueChanged;

        /// <summary>
        /// Gets/sets the cell's value.
        /// </summary>
        public TValue Value 
        {
            get {
                return _value;
            }
            set {
                var newValue = value;
                var oldValue = _value;

                _value = newValue;

                if (OnValueChanged != null) {
                    OnValueChanged(this, new GridCellValueChangedEventArgs<TValue>(oldValue, newValue));
                }
            }
        }
        private TValue _value;

        /// <summary>
        /// Creates a new <see cref="GridCell"/> object.
        /// </summary>        
        public GridCell(TValue value)
        {
            this.Value = value;
        }
        /// <summary>
        /// Creates a new <see cref="GridCell"/> object.
        /// </summary>
        public GridCell() : this(default(TValue)) { }
    }
}
